// ActionScript file

private function getMessages():void {
	var sql:SQLStatement = new SQLStatement();
	sql.sqlConnection = DBConnection;
	sql.text = "SELECT * FROM ChatList";
	sql.execute();
	sql.addEventListener(SQLEvent.RESULT, function():void {InsertMessages(sql)});
}

private function InsertMessages(sql:SQLStatement):void {
	var R:SQLResult = sql.getResult();
	var Data:Array = R.data;
	var old:String = MessagesTA.htmlText;
	MessagesTA.htmlText = "";
	if(Data != null) {
		for(var i:uint = 0; i<Data.length; i++) {
			MessagesTA.htmlText += "<B>"+Data[i].FromUser +" says: </b>" + Data[i].ChatText + "\n";
		}
		MessagesTA.validateNow();
		MessagesTA.verticalScrollPosition = MessagesTA.maxVerticalScrollPosition;
	}
	
}

private function SendMessage():void {
	var sql:SQLStatement = new SQLStatement();
	sql.sqlConnection = DBConnection;
	sql.text = "INSERT INTO ChatList (FromUser, ChatText, TimeStamp) VALUES ('"+CurrentNickName +"', '"+ MessageSentTF.text +"', '"+new Date().toDateString()+"');";
	sql.execute();
	sql.addEventListener(SQLEvent.RESULT, function():void {
		getMessages(); 
		MessageSentTF.text = "";		
	});
	showNotification("New Message", CurrentNickName + " says: " + MessageSentTF.text);
}
			
